package grame;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.NotNull;

/**
 * Controller（Wildcard）Permission
 *
 * @author "Shining Wang<shiningwang@topca.com>"
 */
@SuppressWarnings("unused")
public class ControllerPermission {
    public static final String PERMISSION_TARGET_CONTROLLERS = "controllers";
    public static final String NO_NAMESPACE = "-";
    public static final String ANY_NAMESPACE = "*";
    public static final String ANY_CONTROLLER = "*";
    public static final String ANY_ACTION = "*";
    private static final Logger log = LoggerFactory.getLogger(ControllerPermission.class);

    /**
     * @param controllerName controller name
     * @param actionName     controller action name 参数：null等价于ANY_ACTION；空字符串等价于“index”；
     */
    public static String toPermission(@NotNull String controllerName, String actionName) {
        return toPermission(NO_NAMESPACE, controllerName, actionName);
    }

    /**
     * @param namespace      controller namespace
     * @param controllerName controller name
     * @param actionName     controller action name 参数：null代表ANY_ACTION；空字符串等价于“index”；
     */
    public static String toPermission(@NotNull String namespace, String controllerName, String actionName) {
        if (namespace == null || namespace.isEmpty())
            namespace = ANY_NAMESPACE;
        if (controllerName == null || controllerName.isEmpty())
            controllerName = ANY_CONTROLLER;
        if (actionName == null || actionName.isEmpty())
            actionName = ANY_ACTION;

        log.trace("Create permission with namespace:" + namespace + " controllerName:" + controllerName + " actionName:" + actionName);
        String permission = PERMISSION_TARGET_CONTROLLERS + ":" + namespace + ":" + controllerName + ":" + actionName;
        log.trace("wildcard controller permission result:" + permission);
        return permission;
    }
}
